﻿namespace _01.MobileDevice
{
    using System;

    public class Call
    {
        private string dialedPhoneNumber;
        private TimeSpan duration = TimeSpan.Zero;

        public Call(string dialedPhoneNumber)
        {
            this.DateTime = DateTime.Now;
            this.DialedPhoneNumber = dialedPhoneNumber;
        }

        public Call(DateTime dateTime, string dialedPhoneNumber, TimeSpan duration)
        {
            this.DateTime = dateTime;
            this.DialedPhoneNumber = dialedPhoneNumber;
            this.Duration = duration;
        }

        public DateTime DateTime { get; set; }

        public string DialedPhoneNumber
        {
            get
            {
                return this.dialedPhoneNumber;
            }

            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    throw new NullReferenceException("The Dialed phone can't be null.");
                }

                this.dialedPhoneNumber = value;
            }
        }

        public TimeSpan Duration
        {
            get
            {
                return this.duration;
            }

            set
            {
                if (value.Equals(TimeSpan.Zero))
                {
                    throw new ArgumentNullException("Duration can't be zero.");
                }

                this.duration = value;
            }
        }

        public override int GetHashCode()
        {
            unchecked
            {
                int result = 17;
                result = (result * 23) + ((this.dialedPhoneNumber != null) ? this.dialedPhoneNumber.GetHashCode() : 0);
                result = (result * 23) + this.duration.GetHashCode();
                result = (result * 23) + this.DateTime.GetHashCode();

                return result;
            }
        }

        public bool Equals(Call value)
        {
            if (ReferenceEquals(null, value))
            {
                return false;
            }

            if (ReferenceEquals(this, value))
            {
                return true;
            }

            return object.Equals(this.dialedPhoneNumber, value.dialedPhoneNumber) &&
                   this.duration.Equals(value.duration) &&
                   this.DateTime.Equals(value.DateTime);
        }

        public override bool Equals(object obj)
        {
            Call temp = obj as Call;
            if (temp == null)
            {
                return false;
            }

            return this.Equals(temp);
        }

        public override string ToString()
        {
            return string.Format("Date: {0:dd/MM/yy}, Time: {1:hh:mm:ss}, Phone Number: {2}, Duration: {3}", this.DateTime.Date, this.DateTime, this.dialedPhoneNumber, this.Duration);
        }
    }
}
